<?php
!function_exists('zeai_alone') && exit('forbidden');
require_once ZEAI.'cache/config_login.php';
require_once ZEAI.'cache/config_reg.php';
if(!empty($_LOGIN['mj_domain']) && !empty($_LOGIN['mj_secret'])){
	$userAgent = $_SERVER['HTTP_USER_AGENT'];
	$info = strstr($userAgent, "MAGAPPX");
	$info = explode("|",$info);
	$mj_token = $info[7];
	$mj_url = $_LOGIN['mj_domain'].'/mag/cloud/cloud/getUserInfo?token='.$mj_token.'&secret='.$_LOGIN['mj_secret'];
	$mj_data = get_contents($mj_url);
	$mj_data = json_decode($mj_data,true);
	if($mj_data['success']){
		//$photo_s_url = $mj_data['data']['head'];
		$nickname    = dataIO($mj_data['data']['name'],'in',50);
		$sex         = intval($mj_data['data']['sex']);
		$mob         = $mj_data['data']['phone'];
		$openid      = $mj_data['data']['weixin_openid'];
		$unionid     = $mj_data['data']['weixin_unionid'];
		$magapp_uid  = intval($mj_data['data']['user_id']);
		$dbret = false;
		if (str_len($openid)>10){
			$dbret = dbchk("openid<>'' AND openid='$openid'");
		}
		if(!$dbret && str_len($unionid)>10){
			$dbret = dbchk("unionid<>'' AND unionid='$unionid'");
		}
		if(!$dbret && ifmob($mob)){
			$dbret = dbchk("mob<>'' AND mob='$mob'");
		}
		if(!$dbret){
			$regkind = 13;
			$flag    = 2;
			$ip=getip();
			$reg_if2   = 9999;
			$sjtime    = 0;
			$sjtime2   = 0;
			$reg_grade = (ifint($_REG['reg_grade']) && $_REG['reg_grade']<=20)?intval($_REG['reg_grade']):1;
			$row = $db->ROW(__TBL_ROLE__,"if2","kind=1 AND grade=".$reg_grade,"num");
			if ($row)$reg_if2= $row[0];
			if($reg_grade>1){$sjtime=ADDTIME;$sjtime2=ADDTIME+86400*$reg_if2;}
			$uname = 'mj_'.cdstr(5);
			$pwd = md5($mob);
			if(ifmob($mob))$RZ='mob';
			$db->query("INSERT INTO ".__TBL_USER__." (magapp_uid,sex,mob,nickname,flag,uname,pwd,grade,if2,sjtime,sjtime2,regtime,endtime,regip,endip,refresh_time,regkind,openid,unionid,RZ) VALUES ($magapp_uid,'$sex','$mob','$nickname',$flag,'".$uname."','".$pwd."',$reg_grade,$reg_if2,$sjtime,$sjtime2,".ADDTIME.",".ADDTIME.",'$ip','$ip',".ADDTIME.",$regkind,'$openid','$unionid','$RZ')");
			$uid = intval($db->insert_id());
			shalou_init($uid);
			//
			$row_shalou=shalou_data($uid);
			$row_shalou['photo_s'] = '';
			$jsonstr = encode_json($row_shalou);
			$db->query("UPDATE ".__TBL_SHALOU__." SET jsonstr='$jsonstr' WHERE uid=".$uid);
			set_data_ed_bfb($uid);
			$dbret = dbchk("id=".$uid);
		}
		if($dbret){
			$cook_uid = $dbret['id'];
			$cook_pwd = $dbret['pwd'];
			$cook_sex = $dbret['sex'];
			$cook_photo_s = $dbret['photo_s'];
			$cook_nickname = $dbret['nickname'];
			$cook_grade = $dbret['grade'];
			$cook_flag = $dbret['flag'];
			if($cook_flag==2){
				$jmpurl=($_REG['reg_style']==1)?'reg_alone':'reg_diy';
				header("Location: ".HOST."/m/".$jmpurl.".php");exit;
			}
		}
	}else{
		//exit($mj_data['msg']);
		header("Location: ".HOST."/m/login.php");exit;
	}
}
function dbchk($where) {
	global $db,$_ZEAI,$magapp_uid;
	$row = $db->ROW(__TBL_USER__,"id,nickname,pwd,sex,photo_s,grade,flag",$where,"name");
	if ($row){
		setcookie("cook_uid",$row['id'],null,"/",$_ZEAI['CookDomain']);
		setcookie("cook_pwd",$row['pwd'],null,"/",$_ZEAI['CookDomain']);
		setcookie("cook_sex",$row['sex'],null,"/",$_ZEAI['CookDomain']);
		setcookie("cook_photo_s",$row['photo_s'],null,"/",$_ZEAI['CookDomain']);
		setcookie("cook_nickname",dataIO($row['nickname'],'out'),null,"/",$_ZEAI['CookDomain']);
		setcookie("cook_grade",$row['grade'],null,"/",$_ZEAI['CookDomain']);
		$uid = $row['id'];
		$db->query("UPDATE ".__TBL_USER__." SET endtime=".ADDTIME.",endip='$loginip',magapp_uid='$magapp_uid',logincount=logincount+1 WHERE id=".$uid);
		return $row;
	}else{
		return false;
	}
}
?>